<template>
    <div>
        <div id="postComment">
            <h3>提交评论</h3>
            <hr/>
            <textarea placeholder="请输入您想提交的内容" v-model="postContent"></textarea>
            <mt-button type="primary" size="large" @click="postComment">提交</mt-button>
        </div>
    
        <div id="getComment">
            <h3>评论列表</h3>
            <hr/>
            <div v-for="(item,index) in commentList" :key="index">
                <div class="title">
                    <span>第{{index+1}}楼</span>&nbsp;
                    <span>用户：{{item.user_name}}</span>&nbsp;
                    <span>{{item.add_time | datefmt("YYYY-MM-DD HH:mm:ss")}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell">{{item.content}}</li>
                </ul>
            </div>
            <mt-button type="danger" size="large" @click="moreComment" id="danger">{{more}}</mt-button>
        </div>
    </div>
</template>
<script>

//导入全局域名
import common from '../../kits/common.js';

//导入提示小组件
import { Toast } from 'mint-ui';

export default {
    data() {
        return {
            postContent: "",  //存储用户输入的评论
            more: "点击加载更多 >>",
            commentList: [],    //存储评论内容
            pageCount: 1  //记录页数
        }
    },
    props: ["id"],

    created() {
        //默认显示第一页的评论
        this.getComment();
    },
    methods: {

        //提交评论
        postComment() {
            //如果没有内容不进行提交
            if (this.postContent.trim().length == 0) {
                Toast({
                    message: "请填写评论内容之后再提交",
                    duration: 1000
                })
                return;
            }

            //通过api提交数据
            this.$http.post(common.urlHost + "/api/postcomment/" + this.id,
                { content: this.postContent }, { emulateJSON: true }).then(function (res) {
                    Toast({
                        message: res.body.message,
                        duration: 3000
                    });
                    this.postContent = ""  //提交成功后将输入框清空
                    this.getComment()   //重新获得数据进行更新
                })

        },

        getComment(pageCount) {
            //如果是提交了数据默认显示第一页数据  清空存评论内容的数组
            if (!pageCount) {
                this.pageCount = 1;
                this.commentList = [];
            }

            //获取数据
            this.$http.get(common.urlHost + "/api/getcomments/" + this.id + "?pageindex=" + this.pageCount).then(function (res) {

                //判断数据是否加载完毕
                this.more = res.body.message.length < 10 ? "没有更多了" : "点击加载更多 >>";


                this.commentList = this.commentList.concat(res.body.message)
            })
        },
        moreComment() {
            this.pageCount++;
            this.getComment(this.pageCount);
        }
    }
}
</script>
<style scoped>
#getComment,
#postComment {
    margin: 20px 0;
}

h3 {
    margin: 5px 0;
}

#getComment ul li {
    list-style: none;
}

.title {
    padding: 5px;
    color: #6d6d72;
    font-size: 15px;
    background-color: rgba(0, 0, 0, 0.1)
}

#danger {
    background-color: transparent;
    border: 1px solid #e92322;
    color: #e92322;
}
</style>


